package cn.iocoder.yudao.module.train.controller.wechat.coursecommodity;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.train.controller.admin.coursecommodity.vo.CourseCommodityPageReqVO;
import cn.iocoder.yudao.module.train.controller.admin.coursecommodity.vo.CourseCommodityRespVO;
import cn.iocoder.yudao.module.train.controller.admin.order.vo.OrderPageReqVO;
import cn.iocoder.yudao.module.train.controller.admin.student.vo.StudentPageReqVO;
import cn.iocoder.yudao.module.train.controller.admin.student.vo.StudentRespVO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseCommodityDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StudentUserDTO;
import cn.iocoder.yudao.module.train.dal.dataobject.order.OrderDO;
import cn.iocoder.yudao.module.train.dal.mysql.order.OrderMapper;
import cn.iocoder.yudao.module.train.service.coursecommodity.CourseCommodityService;
import cn.iocoder.yudao.module.train.service.order.OrderService;
import cn.iocoder.yudao.module.train.service.student.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.GET;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "小程序接口 - 课程商品管理")
@RestController
@RequestMapping("/wechat-api/course-commodity")
@Validated
public class WechatCourseCommodityController {

    @Resource
    private CourseCommodityService courseCommodityService;

    @Resource
    private OrderService orderService;

    @Resource
    private StudentService studentService;

    @GetMapping("/get")
    @Operation(summary = "获得课程商品管理")
    public CommonResult<CourseCommodityRespVO> getCourseCommodity(Long commodityId) {
        if (commodityId == null) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        CourseCommodityDTO courseCommodity = courseCommodityService.getCourseCommodity(commodityId);
        return success(BeanUtils.toBean(courseCommodity, CourseCommodityRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得课程商品管理列表")
    @ApiAccessLog(operateType = GET)
    public CommonResult<List<CourseCommodityRespVO>> getCourseCommodityList(@Valid CourseCommodityPageReqVO pageReqVO) {
        List<CourseCommodityDTO> listResult = courseCommodityService.getCourseCommodityList(pageReqVO);
        return success(BeanUtils.toBean(listResult, CourseCommodityRespVO.class));
    }



    @GetMapping("/listStudents")
    @Operation(summary = "获得商品课程学生列表")
    @ApiAccessLog(operateType = GET)
    public CommonResult<List<StudentRespVO>> listStudents(Long commodityId) {

        if (commodityId == null) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        List<StudentUserDTO> list = new ArrayList<>();
        for (OrderDO orderDO : orderService.getOrderList(new OrderPageReqVO().setCommodityId(commodityId))) {
            StudentUserDTO student = studentService.getStudent(orderDO.getStudentId());
            list.add(student);
        }

        return success(BeanUtils.toBean(list, StudentRespVO.class));
    }

    @GetMapping("/listByStudentId")
    @Operation(summary = "获得学生商品课程列表")
    @ApiAccessLog(operateType = GET)
    public CommonResult<List<CourseCommodityRespVO>> listByStudentId(Long studentId) {

        if (studentId == null) {
            return CommonResult.error(GlobalErrorCodeConstants.BAD_REQUEST);
        }
        List<CourseCommodityDTO> list = new ArrayList<>();
        for (OrderDO orderDO : orderService.getOrderList(new OrderPageReqVO().setStudentId(studentId))) {
            CourseCommodityDTO student = courseCommodityService.getCourseCommodity(orderDO.getCommodityId());
            list.add(student);
        }

        return success(BeanUtils.toBean(list, CourseCommodityRespVO.class));
    }
}
